<style scoped>
  .weui-form-preview__hd label, .weui-form-preview__hd em {
    font-size: .9em !important;
  }
  .rj-cell.max span{
    font-size: 16px;
    padding-right: 20px;
  }
  .rj-cell label {
    color: #333;
    font-size: 14px;
    width: 44%;
    display: inline-block;
    padding: 5px 3%;
    text-align: right;
  }
  .rj-cell span {
    font-size: 14px;
    color: #333;
    display: inline-block;
    width: 50%;
  }
  .rj-cell.note label{
    float: left;
  }
  .rj-cell.note span{
    width: 80%;
    display: inline-block;
  }
  .weui-cell {
    background: #fff;
  }
  #acts span {
    font-size: 14px;
  }
  .weui-label{
    /*width: 130px;*/
    text-align: left;
  }

  .weui-select {
    height: auto;
    line-height: normal;
  }
  *{
    list-style:none;
  }
  /*body{*/

  /*}*/
  .weui-cell_select {
    padding: 10px 15px;
  }
  .weui-cells__title {
    text-align: left;
  }
  .hide {
    display: none;
  }
  #acts {
    font-size: 0;
  }
  #acts span {
    font-size: 14px;
  }
</style>
<template>
  <div class="oitem">
    <div class="weui-cells__title">货品明细{{itemNo + 1}}
      <p id="acts" style="float: right;color: #1aad19;" v-if="isLast">
        <span id="addItem" @click="addItem" style="padding-right: 5px;">添加</span>
        <span v-if="itemNo != 0">|</span>
        <span id="delItem" style="padding-left: 5px;" @click="delItem" v-if="itemNo != 0">删除</span>
      </p>
    </div>
    <div class="weui-cell weui-cell_select fir">
      <div class="weui-cell__hd"><label class="weui-label">货品类别</label></div>
      <div class="weui-cell__bd">
        <select class="weui-select type" :name="'items[' + itemNo + '].picktype'" v-model="atype">
          <option v-for="(item, index) in picktypes" :value="item.dictkey" :disabled="packPtype(item.dictkey)">{{item.dicttext}}</option>
        </select>
      </div>
    </div>
    <div class="weui-cell weui-cell_select">
      <div class="weui-cell__hd"><label class="weui-label">产品</label></div>
      <div class="weui-cell__bd">
        <select class="weui-select product" :name="'items[' + itemNo + '].productid'" v-model="currentProduct">
          <option value="" disabled>请选择</option>
          <option   v-for="(item, index) in products" :key="item.guid" :value="item.guid">{{item.productname}}</option>
        </select>
      </div>
    </div>
    <div class="weui-cell weui-cell_select">
      <div class="weui-cell__hd"><label class="weui-label">规格 </label></div>
      <div class="weui-cell__bd">
        <input type="hidden" class="spec"  :name="'items[' + itemNo + '].speid'" :value="spec.guid" />
        <input type="hidden"  :name="'items[' + itemNo + '].spename'" :value="spec.name" />
        <input type="hidden"  :name="'items[' + itemNo + '].stockunit'" :value="spec.stockunit" />
        <input type="hidden"  :name="'items[' + itemNo + '].stockunitid'" :value="spec.stockunitid" />
<!--        <select class="weui-select spec" v-model="spec">-->
<!--          <option selected=""  v-for="(item, index) in specifications" :key="item.guid" :value="item">{{item.name}}</option>-->
<!--        </select>-->
        <model-list-select :list="specifications"
                           v-model="specId"
                           option-value="guid"
                           option-text="name"
                           placeholder="请选择">
        </model-list-select>
      </div>
    </div>
    <div class="weui-cell">
      <div class="weui-cell__hd"><label class="weui-label">{{spec.isUnit == 1 ? '重量' : '数量'}}</label></div>
      <div class="weui-cell__bd">
        <input class="weui-input weight inlinput1" type="number" :name="'items[' + itemNo + '].weight'" placeholder="请输入" />
        <input class="weui-input inlinput2" type="text" readonly :value="spec.stockunit" />
      </div>
    </div>
    <template v-if="picktype == 2">
      <div class="weui-cells__title">成本对象</div>
      <div class="weui-cell weui-cell_select fir">
        <div class="weui-cell__hd"><label class="weui-label">产品</label></div>
        <div class="weui-cell__bd">
          <select class="weui-select product2" v-model="product2">
            <option value="" disabled>请选择</option>
            <option   v-for="(item, index) in products2" :key="item.productId" :value="item.productId">{{item.productName}}</option>
          </select>
        </div>
      </div>
      <div class="weui-cell weui-cell_select">
        <div class="weui-cell__hd"><label class="weui-label">规格 </label></div>
        <div class="weui-cell__bd">
          <select class="weui-select spec2" :name="'items[' + itemNo + '].cospguid'">
            <option selected=""  v-for="(item, index) in specifications2" :key="item.guid" :value="item.guid">{{item.name}}</option>
          </select>
        </div>
      </div>
    </template>
    <div class="weui-cells__title"></div>
    <div class="weui-cell fir">
      <div class="weui-cell__hd"><label class="weui-label">备注</label></div>
      <div class="weui-cell__bd">
        <textarea class="weui-textarea" :name="'items[' + itemNo + '].remark'" placeholder="请输备注内容" rows="2"></textarea>
      </div>
    </div>
  </div>
</template>

<script>
  import 'vue-search-select/dist/VueSearchSelect.css'
  import {ModelListSelect} from 'vue-search-select'
export default {
  name: 'PickItem',
  data () {
    return {
      value: '',
      defaultResult: [],
      currentProduct: '',
      specifications: [],
      spec: '',
      picktypes: [],
      atype: '',
      products: [],
      product2: '',
      specifications2: [],
      specId: ''
    }
  },
  components: {ModelListSelect},
  props: [
    'itemNo',
    'isLast',
    'picktype',
    'products2'
  ],
  computed: {
    filterResult () {
      return this.defaultResult.filter(value => new RegExp(this.value, 'i').test(value))
    }
  },
  watch: {
    'picktype': function (val) {
      if (val == 1) {
        this.atype = 1;
      } else if (val == 2) {
        this.atype = 4;
      }
    },
    'currentProduct': function(){
      this.getSpecByPro();
    },
    'atype': function (val) {
      this.getProducts(val);
      // this.getSpecByPro();
      this.specifications = [];
    },
    'product2': function (val) {
      this.getSpecByPro2(val);
    },
    'specId': function (val) {
      if (!val) return;
      this.spec = this.specifications.filter(item => item.guid == val)[0];
      this.$forceUpdate();
    },
  },
  mounted() {
    this.getPicktypes();
  },
  methods: {
    packPtype: function(dictkey) {
      let res = true;
      if (this.picktype == 1) {
        if (dictkey == 1 || dictkey == 2 || dictkey == 3) {
          res = false
        } else if (dictkey == 4) {
          res = true;
        }
      } else if (this.picktype == 2) {
        if (dictkey == 1 || dictkey == 2 || dictkey == 3) {
          res = true
        } else if (dictkey == 4) {
          res = false;
        }
      }
      return res;
    },
    getPicktypes: async function() {
      await this.picktype;
      this.axios({
        method: 'get',
        responseType: 'json',
        url: '/sales/Sellform!findDict.action',
        params: {type: 26}
      }).then(response => {
        if (response.statusText == "OK") {
          this.picktypes = response.data;
          this.atype = this.picktype == 2 ? 4 : 1;
        } else {
          this.toastText('获取领料类型数据失败');
        }
      });
    },
    getProducts: function(type) {
      let ptype = type == 2 ? '2,5' : type;
      this.axios({
        method: 'get',
        url: '/sales/Sellform!findProductinfo.action',
        params: {productflag: ptype}
      }).then(response => {
        if (response.data.status == "true") {
          this.products = response.data.result;
        } else if (response != undefined && response.data != undefined && response.data.msg == 'sessionTimeOut') {
          //不不设置提示
        } else {
          this.toastText('获取物料数据失败')
        }
      });

    },
    getSpecByPro: function() {
      this.axios({
        method: 'get',
        url: '/produce/Whinapply!getSpeByproduct.action',
        params: {productid: this.currentProduct, type: this.atype, stats: '1,4'}
      }).then((response) => {
        if (response.data.msg == 'success') {
          let data = response.data;
          this.specifications = data.specifications;
        } else {
          this.toastError("获取规格数据失败");
        }

      })
    },
    getSpecByPro2: function() {
      this.axios({
        method: 'get',
        url: '/produce/Whinapply!getSpeByproduct.action',
        params: {productid: this.product2, types: '2,3', stats: '1,4'}
      }).then((response) => {
        if (response.data.msg == 'success') {
          let data = response.data;
          this.specifications2 = data.specifications;
        } else {
          this.toastError("获取成本控制规格数据失败");
        }

      })
    },
    addItem: function() {
      this.$emit('doAddItem');
    },
    delItem: function() {
      this.$emit('doDelItem');
    },
    onSearch (val) {
      this.$emit('doSearch',val)
    },
    onCancel () {
      this.$emit('doSearch', '')
    },
    onSelect (val) {
      this.value = val;
    }
  }
}
</script>
